<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>消息滚动切换(Message scrolling switching)</title>
  <style>
    .order-tip-bar, .random-tip-bar {
      display: inline-flex;
      align-items: center;
      height: 30px;
      background-color: rgba(0, 0, 0, .4);
      border-radius: 16px;
    }

    img {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      margin-right: 5px;
    }

    span {
      color: #fff;
      font-size: 12px;
      margin-right: 8px;
    }
  </style>
</head>
<body>

  <h3>1. 顺序切换</h3>
  <div class="order-tip-bar">
    <img class="order-avatar" src="https://bfs.biyao.com/group1/M01/A2/67/rBACVGA_iOuAYaTxAAAPbted3yE165.png" alt="">
    <span class="order-tip">178***7678对这件商品很感兴趣</span>
  </div>

  <h3>2. 随机切换</h3>
  <div class="random-tip-bar">
    <img class="random-avatar" src="https://bfs.biyao.com/group1/M01/A2/67/rBACVGA_iOuAYaTxAAAPbted3yE165.png" alt="">
    <span class="random-tip">178***7678对这件商品很感兴趣</span>
  </div>

  <script>

    let tipList = [
      {
        icon: 'https://bfs.biyao.com/group1/M01/A6/97/rBACYWBCHqyAFH5tAAANZXX5Eww646.png',
        title: 'inline-flex 购买了这件商品'
      },
      {
        icon: 'https://bfs.biyao.com/group1/M01/A2/67/rBACVGA_iOuAYaTxAAAPbted3yE165.png',
        title: 'flex 购买了这件商品'
      },
      {
        icon: 'https://bfs.biyao.com/group1/M00/7F/4E/rBACYV16HseAP-PnAAAW9bbVoKE463.png',
        title: 'align-items 购买了这件商品'
      }
    ]

    const orderAvatarEl = document.querySelector('.order-avatar')
    const orderTipEl = document.querySelector('.order-tip')
    const randomAvatarEl = document.querySelector('.random-avatar')
    const randomTipEl = document.querySelector('.random-tip')
    
    // 1. 定时切换
    let index = 0
    setInterval(() => {
      index++
      if (index >= tipList.length) {
        index = 0
      }
      orderAvatarEl.src = tipList[index].icon
      orderTipEl.innerText = tipList[index].title
    }, 2000)

    // 2. 随机切换
    let randomIndex = 0
    let recordIndex = 0
    setInterval(() => {
      randomIndex = Math.floor(Math.random() * tipList.length)
      if (randomIndex === recordIndex) {
        randomIndex = (randomIndex + 1) % tipList.length
      }
      recordIndex = randomIndex
      randomAvatarEl.src = tipList[randomIndex].icon
      randomTipEl.innerText = tipList[randomIndex].title
    }, 2000)

    
  </script>
  
</body>
</html>